/**
 * Created by hcadmin on 16/10/17.
 */

//时间
var tempTime = {};
var min_sec;            //分:秒
var whole_time;         //2016.10.26 12:33
var hours_min;          //时:分
var whole_day;          //2016.10.26
var year_mon_day;       //2016-10-26
var day_min;            //10月26日 12:33
var year_min;           //2016年10月26日 12:33
var year_whole;         //2016-10-26 12:33
var timestamp;           //时间戳
function initDate(date){
    tempTime = {
        year   : date.getFullYear(),
        month  :(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1),
        day    :date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ,
        hours  :date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
        min    :date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
        sec    :date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(),
    };
    whole_time = tempTime.year+"."+tempTime.month+"."+tempTime.day+" "+tempTime.hours+":"+tempTime.min;
    whole_day  = tempTime.year+"."+tempTime.month+"."+tempTime.day;
    hours_min  = tempTime.hours+":"+tempTime.min;
    min_sec    = tempTime.min+":"+tempTime.sec;
    mon_day = tempTime.month+"-"+tempTime.day;
    year_mon_day = tempTime.year+"-"+tempTime.month+"-"+tempTime.day;
    day_min    = tempTime.month+"月"+tempTime.day+"日"+" "+tempTime.hours+":"+tempTime.min;
    year_min  = tempTime.year+"年"+tempTime.month+"月"+tempTime.day+"日"+" "+tempTime.hours+":"+tempTime.min;
    year_whole = tempTime.year+"-"+tempTime.month+"-"+tempTime.day+" "+tempTime.hours+":"+tempTime.min;
}

//时间转换时间戳
function initTimestamp(date){
    var time = date;
    time = new Date(Date.parse(new Date(date)));
    timestamp = time.getTime();
}
//获取
//数据展示方法...
/*
* id         数据展示的div
* user_id    当前行的用户id
* listType   列表类型
* which_page 模块("我审批的","我发起的","抄送我的")
*
* */


function initData(id,user_id,listType,which_page,timestamp,search,keyword){
    //debugger;
    var html;
    var url;
    var data;
    if(search=="1"){
        url = apiurl+"/approvals/search";
        data = {
            searchType:listType,
            user_id:user_id,
            timestamp:timestamp,
            keyword:keyword,
            token:localStorage.token,
            group_id:localStorage.group_id
        };
    }else{
        url =apiurl+"/approvals/list";
        data = {
            listType:listType,
            user_id:user_id,
            timestamp:timestamp,
            token:localStorage.token,
            group_id:localStorage.group_id
        };
    }

    $.ajax({
        beforeSend:function(){
            $(".loading").show();
        },
        url:url,
        type:"GET",
        data:data,
        success:function(d){
            $(".loading").hide();
            //console.log(d);
            if(typeof d == "object"){
                var msg= d.msg;
                if(msg=="202") {
                    $(".token_iframe").fadeIn(1000);
                }
            }
            else
            {
                var dataWhole = JSON.parse(d).data;
                var del_arr1 = [];
                if(dataWhole==undefined){
                    $("#blank_log").show();
                }else if(dataWhole.list.length==0){
                    $("#blank_log").show();
                }
                else{
                    $("#blank_log").hide();
                    var data     = JSON.parse(d).data.list;
                    var userInfo = JSON.parse(d).data.userCallbackString;

                    var html_len = "";
                    for(var i=0;i<data.length;i++){
                        var user_id             = data[i].user_id;
                        var user;
                        var user_icon;
                        var user_qyxBindId;
                        for(var j=0;j<userInfo.length;j++){
                            if(userInfo[j].id==user_id){
                                user            = userInfo[j].name;
                                user_icon       = userInfo[j].avatar;
                                user_qyxBindId  = userInfo[j].qyxBindId;
                            }
                        }
                        var avatar_img;
                        //if(user_icon=="") {
                            var color = avatar_getColor(user_qyxBindId);
                            avatar_img = "<div class='left_pic' style='background-color: "+color+"'>"+avatar_name(user)+"</div>"
                        //}else{
                        if (user_icon.length > 0){
                            var src = avatar_getUrl(localStorage.user_id,localStorage.token,user_icon);
                            //avatar_img = "<img class='left_pic' src='../../approval/approvalImg/contact.png'>";
                            avatar_img += "<img class='left_pic' src='"+src+"' hidden>";
                        }
                        var approval            = data[i].approval_id;
                        var approval_status     = data[i].approval_status;
                        var del_flag            = data[i].del_flag;
                        var time                = data[i].update_timestamp;
                        var title               = data[i].approval_title;
                        var current_approver    = data[i].current_approver;
                        var current_name;
                        var loginName;
                        var email;
                        var phone;
                        var qyxBindId;


                        var is_today = false;

                        var today = new Date();
                        var today_year = today.getFullYear();
                        var today_month = today.getMonth();
                        var today_day = today.getDate();

                        var approval_time = new Date(time);

                        if (today_year == approval_time.getFullYear() && today_month == approval_time.getMonth() && today_day == approval_time.getDate()){
                            is_today = true;
                        }


                        for(var a=0;a<userInfo.length;a++){
                            if(userInfo[a].id==current_approver){
                                current_name = userInfo[a].name;
                                loginName    = userInfo[a].loginName;
                                email        = userInfo[a].email;
                                phone        = userInfo[a].phone;
                                qyxBindId    = userInfo[a].qyxBindId;

                            }
                        }
                        var status;
                        var index;
                        var img_html            = "";
                        if(del_flag=="1"){
                            status   ="已撤回";
                            img_html = "";
                        }else{
                            del_arr1.push(del_flag);
                            if(approval_status=="0"){
                                if(listType=="0"){
                                    status="待审批";
                                }
                                if(which_page=="2"||listType=="1"){
                                    status="等待"+current_name+"审批";
                                }
                                img_html  = "<img src='../approvalImg/ping.png' name='"+user+"的"+title+"'  id='"+approval+"' id2='"+current_approver+"' userName='"+current_name+"' loginName='"+loginName+"' email='"+email+"' phone='"+phone+"' qyxBindId='"+qyxBindId+"'  class='little_icon'>";
                            }else if(approval_status=="1"){
                                status="审批完成(同意)";
                                img_html  = "";
                            }else{
                                status="审批完成(拒绝)";
                                img_html  = "";
                            }
                        }
                        //初始化时间

                        var create_timestamp = data[i].create_timestamp;

                        //var date = new Date(data[i].create_timestamp);
                        //initDate(date);
                        if(which_page=="1"){
                            if(del_flag=="1"){
                                html="";
                            }else{

                                var styleClass = "approval_style_grey";
                                if (listType == "0"){
                                    //待我审批的
                                    styleClass = "approval_style";
                                }


                                var timeString = "";
                                if (is_today == false){
                                    timeString = timestampToString(create_timestamp,"MM-dd");
                                }else {
                                    timeString = timestampToString(create_timestamp,"HH:mm");
                                }

                                html="<div class='tableview_cell1' time='"+time+"' del_flag='"+data[i].del_flag+"' id='"+data[i].approval_id+"' status='"+approval_status+"'><div class='left_mid col-xs-2'>"+avatar_img+"</div><div class='right_small_up col-xs-8'><p class='approval_name'>"+user+"的"+title+"</p><p class='"+ styleClass +"'>"+status+"</p></div><div class='right_small'>"+timeString+"</div></div>";
                            }
                        }
                        if(which_page=="2"){
                            var timeString = timestampToString(create_timestamp,"yyyy.MM.dd");
                            html="<div class='tableview_cell1' time='"+time+"' del_flag='"+data[i].del_flag+"' id='"+data[i].approval_id+"' status='"+approval_status+"'><div class='left_mid col-xs-2'>"+avatar_img+"</div><div class='right_small_up col-xs-7' id='"+data[i].approval_id+"' status='"+approval_status+"'><p  class='approval_name'>"+user+"的"+title+"</p><p  class='approval_style_grey'>"+status+"</p></div><div class='right_small col-xs-2'>"+timeString+""+img_html+"</div></div>";
                        }
                        if(which_page=="3"){
                            if(approval_status=="1"){
                                var timeString = timestampToString(create_timestamp,"yyyy.MM.dd");
                                html="<div class='tableview_cell1' time='"+time+"' del_flag='"+data[i].del_flag+"' id='"+data[i].approval_id+"' status='"+approval_status+"'><div class='left_mid col-xs-2'>"+avatar_img+"</div><div class='right_small_up col-xs-7'><p  class='approval_name'>"+user+"的"+title+"</p><p class='approval_style_grey'>审批已完成(同意)</p></div><div class='right_small col-xs-2'>"+ timeString +"</div></div>";
                            }else{
                                html="";
                            }
                        }
                        $("."+id+"").append(html);
                        html_len+=html;
                    }
                }
                if(listType=="0"){
                    $("#waitApproval").html("待我审批的("+del_arr1.length+")");
                    if(html_len.length==0){
                        $("#blank_log").show();
                    }
                }
                if(which_page=="3"||which_page=="2"||which_page=="1"){
                    // 没有 同意的审批  页面显示为空
                    if($(".tableview_cell1").length==0){
                        $("#blank_log").show();
                    }else{
                        $("#blank_log").hide();
                    }
                }
            }

            showAvatar(".left_pic");
        }
    });
}

function showAvatar(classname){
    $(classname).one("load",function(){
        $(this).show();
        $(this).prev().hide();
    });
}